package collections;

import java.util.*;

/**
 * Created by pc on 2015/2/16.
 * 集合框架里放的都是对象的引用，对象一变则全变
 * 字符串比较特殊，来自于常量池* *
 */
public class CollectionArrayReferenceDemo {
    public static void main(String args[]) {
        //can Array store object instances or just reference?
        String[] strArray = new String[4];
        List<String> strList = new ArrayList<String>();

        StringBuilder sb = new StringBuilder();
        sb.append("1111");
        strArray[0] = sb.toString();
        strList.add(sb.toString());

        sb.append("2222");
        strArray[1] = sb.toString();
        strList.add(sb.toString());

        sb.append("3333");
        strArray[2] = sb.toString();
        strList.add(sb.toString());

        System.out.println(Arrays.toString(strArray));
        System.out.println(strList);
        
        System.out.println("===============================");
        //

        Hashtable ht = new Hashtable();
        StringBuilder sb2 = new StringBuilder();
        List<StringBuilder> strList2 = new ArrayList<StringBuilder>();
        StringBuilder[] strArray2 = new StringBuilder[4];


        sb2.append("abc,");
        ht.put("1", sb2);
        strList2.add(sb2);
        strArray2[0] = sb2;

        sb2.append("def,");
        ht.put("2", sb2);
        strList2.add(sb2);
        strArray2[1] = sb2;

        sb2.append("mno,");
        ht.put("3", sb2);
        strList2.add(sb2);
        strArray2[2] = sb2;

        sb2.append("xyz.");
        ht.put("4", sb2);
        strList2.add(sb2);
        strArray2[3] = sb2;

        int numObj = 0;
        Enumeration it = ht.elements();
        while (it.hasMoreElements()) {
            System.out.print("get StringBufffer " + (++numObj) + " from Hashtable: ");
            System.out.println(it.nextElement());
        }
        System.out.println(strList2);
        System.out.println(Arrays.toString(strArray2));

    }
}
